﻿using CL_SQL.BLL;
using CL_SQL.DAL;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace StudentLoan.Controllers
{
    public class typeController : Controller
    {
        typeBLL tb = new typeBLL();
        // GET: type
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult type()
        {
            return View();
        }
        public void ALLtype()
        {
           
            var pageNumber = Convert.ToInt32(Request.Params["page"]);
            var pageSize = Convert.ToInt32(Request.Params["rows"]);

            List<CL_SQL.Type> list = new List<CL_SQL.Type>();
            list = tb.Alltype();
            Hashtable ht = new Hashtable
            {
                {"total",list.Count() },
                {"rows" ,list.Take(pageSize*pageNumber).Skip(pageSize*(pageNumber-1)).ToList<CL_SQL.Type>() }
            };
            HttpContext.Response.Write(JsonConvert.SerializeObject(ht));
        }
        public void Page()
        {
            var pageNumber = Convert.ToInt32(Request.Params["pageNumber"]);
            var pageSize = Convert.ToInt32(Request.Params["pageSize"]);

            List<CL_SQL.Type> list = new List<CL_SQL.Type>();
            list = tb.Alltype();
            Hashtable ht = new Hashtable
            {
                {"total",list.Count() },
                {"rows" ,list.Take(pageSize*pageNumber).Skip(pageSize*(pageNumber-1)).ToList<CL_SQL.Type>() }
            };
            HttpContext.Response.Write(JsonConvert.SerializeObject(ht));
        }
        //编辑
        public void Edit(int ID)
        {
            CL_SQL.Type type = new CL_SQL.Type();
            type.TypeId = ID;
            type.Typename = Request.Form["Typename"];
            type.Sum = Convert.ToSingle(Request.Form["Sum"]);
            type.LastModifyDate =Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            type.LastModifyUser = "张三";
            

            if (tb.Edit(type))
                HttpContext.Response.Write(0);
            else
                HttpContext.Response.Write(1);
            HttpContext.Response.End();
        }
        [HttpPost]
        public void Delete(int ID)
        {
            if (tb.Delete(ID))
                HttpContext.Response.Write(0);
            else
                HttpContext.Response.Write(1);
            HttpContext.Response.End();

        }
        public void Add()
        {
            CL_SQL.Type type = new CL_SQL.Type();
            type.TypeId = Convert.ToInt32(Request.Form["TypeId"]);
            type.Typename = Request.Form["Typename"];
            type.Sum = Convert.ToSingle(Request.Form["Sum"]);
            type.LastModifyDate = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
            type.LastModifyUser = "张三";

            if (tb.Add(type))
                HttpContext.Response.Write(0);
            else
                HttpContext.Response.Write(1);
            HttpContext.Response.End();
        }
    }
}